package com.ltu.timkor6.gamesite.games.memory.client;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Composite;
import com.ltu.timkor6.gamesite.shared.Result;
import com.ltu.timkor6.gamesite.users.client.Users;

public class Memory
{
	//private Users users;
	private MemoryComposite composite;
	
	private MemoryServiceAsync service = GWT.create(MemoryService.class);
	
	public Memory(Users users)
	{
		checkUser();
		composite = new MemoryComposite(this);
	}
	
	public void checkSubmit()
	{
		int size = 0;
		boolean error = false;
		
		try
		{
			size = Integer.parseInt(this.composite.getNumber());
		}
		catch(NumberFormatException e)
		{
			error = true;
			this.composite.addError("You must enter a number.");
		}
		
		if(error == false)
		{
			if(1 < size && size < 9 && size % 2 == 0)
			{
				composite.setMemorySize(size);
				start(size);
			}
			else
			{
				this.composite.addError("This Hanoi game requires a number between 2 and 8.");
			}
		}
	}
	
	/**
	 * For user authentification
	 */
	public void checkUser()
	{
		// Initialize the service proxy.
		if(service == null)
		{
			service = GWT.create(MemoryService.class);
		}
		
		// Set up the callback object.
		AsyncCallback<Boolean> callback = new AsyncCallback<Boolean>()
		{
			public void onFailure(Throwable caught)
			{
				Window.alert("Error: " + caught.getMessage());
			}
			
			public void onSuccess(Boolean signedIn)
			{
			}
		};
		
		// Make the call to the stock price service.
		service.checkUser(callback);
	}
	
	/**
	 * Send start game.
	 *  - Defines number of questions to answer.
	 * @param questions
	 */
	public void start(final int size)
	{
		// Initialize the service proxy.
		if(service == null)
		{
			service = GWT.create(MemoryService.class);
		}
		
		// Set up the callback object.
		AsyncCallback<Boolean> callback = new AsyncCallback<Boolean>()
		{
			public void onFailure(Throwable caught)
			{
				Window.alert("Error: " + caught.getMessage());
			}
			
			public void onSuccess(Boolean ok)
			{
				if(ok)
				{
					composite.setMemorySize(size);
					composite.startGame();
				}
			}
		};
		
		// Make the call to the stock price service.
		service.start(size, callback);
	}
	
	public void image(final int row, final int column)
	{
		// Initialize the service proxy.
		if(service == null)
		{
			service = GWT.create(MemoryService.class);
		}
		
		// Set up the callback object.
		AsyncCallback<Integer> callback = new AsyncCallback<Integer>()
		{
			public void onFailure(Throwable caught)
			{
				Window.alert("Error: " + caught.getMessage());
			}
			
			public void onSuccess(Integer value)
			{
				composite.setValue(row, column, value);
			}
		};
		
		// Make the call to the stock price service.
		service.image(row, column, callback);
	}
	
	/**
	 * Send start game.
	 *  - Defines number of questions to answer.
	 * @param questions
	 */
	public void finished()
	{
		// Initialize the service proxy.
		if(service == null)
		{
			service = GWT.create(MemoryService.class);
		}
		
		// Set up the callback object.
		AsyncCallback<Result> callback = new AsyncCallback<Result>()
		{
			public void onFailure(Throwable caught)
			{
				Window.alert("Error: " + caught.getMessage());
			}
			
			public void onSuccess(Result result)
			{
				composite.finished(result);
			}
		};
		
		// Make the call to the stock price service.
		service.finished(callback);
	}
	
	public Composite getComposite()
	{
		return composite;
	}
}
